<?php

namespace app\modules\merchant\controllers;

use app\common\controllers\AccessController;
use app\modules\merchant\forms\category\GoodsCategoryForm;
use app\modules\merchant\services\GoodsCategoryService;
use yii\base\UserException;

class GoodsCategoryController extends AccessController {

    //显示
    public function actionSearch(){
        return GoodsCategoryService::search(\Yii::$app->getModule("merchant")->get("user")->getId());
    }

    //添加
    public function actionAdd(){
        $form = new GoodsCategoryForm();
        $form->setScenario("create");
        if($form->load(\Yii::$app->getRequest()->post(),'') && GoodsCategoryService::create($form)){
           return "";
        }else{
            throw new UserException(GoodsCategoryService::getError());
        }
    }

    //获取信息
    public function actionGetInfoById($id){
        return GoodsCategoryService::getInfoById(\Yii::$app->getModule("merchant")->get("user")->getId(),$id);
    }

    //编辑
    public function actionEdit(){
        $form = new GoodsCategoryForm();
        $form->setScenario("update");
        if($form->load(\Yii::$app->getRequest()->post(),'') && GoodsCategoryService::update($form)){
            return "";
        }else{
            throw new UserException(GoodsCategoryService::getError());
        }
    }

    //删除
    public function actionDelete($id){
        GoodsCategoryService::delete(\Yii::$app->getModule("merchant")->get("user")->getId(),$id);
        return "";
    }
}